﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using JQuery.WebKit.Base;
using JQuery.WebKit.WebCommon;
using JQuery.WebKit.WebCommon.Theming;

[assembly: WebResource("JQuery.WebKit.ThemeSwitcher.ThemeSwitcher.js", "text/javascript")]
[assembly: WebResource("JQuery.WebKit.ThemeSwitcher.ThemeSwitcher.min.js", "text/javascript")]
namespace JQuery.WebKit
{
    public class ThemeSwitcher : WebControlBase
    {
        private ListBox _themeList;
        HtmlButton _btnApply;

        public int Rows { get; set; }

        public ThemeSwitcher()
            : base("table")
        {
            _themeList = new ListBox();
            _themeList.ID = "innerList";

            _btnApply = new HtmlButton();
            _btnApply.ID = "applyTheme";
            _btnApply.InnerText = "Apply";
        }

        protected override void CreateChildControls()
        {
            List<Theme> themeList = ThemeManager.GetThemes(false);
            HtmlTableCell cellRight = new HtmlTableCell();

            foreach (Theme theme in themeList)
            {
                ListItem item = new ListItem(string.IsNullOrEmpty(theme.DisplayName) ? theme.Name : theme.DisplayName, theme.Name);
                Image previewImage = new Image();
                if (theme.Name == Page.Theme)
                {
                    item.Selected = true;
                }
                else
                {
                    previewImage.Style.Add(HtmlTextWriterStyle.Display, "none");
                }
                _themeList.Items.Add(item);
                previewImage.ImageUrl = theme.PreviewImagePath;
                previewImage.AlternateText = theme.Name;
                cellRight.Controls.Add(previewImage);
            }

            HtmlTableRow row1 = new HtmlTableRow();
            base.Controls.Add(row1);
            HtmlTableCell cellLeft = new HtmlTableCell();
            //cellLeft..Width = "30%";
            cellLeft.Attributes.Add("class", "themeList");
            
            cellRight.Attributes.Add("class", "themePreview");
            cellRight.Align = "center";
            _themeList.Width = Unit.Percentage(100);
            _themeList.Height = Unit.Percentage(100);
            _themeList.Rows = Rows > 0 ? Rows : 20;

            //cellRight.Align = "center"
            row1.Cells.Add(cellLeft);
            row1.Cells.Add(cellRight);
            cellLeft.Controls.Add(_themeList);

            HtmlTableRow row2 = new HtmlTableRow();
            base.Controls.Add(row2);
            HtmlTableCell cellApply = new HtmlTableCell();
            cellApply.Attributes.Add("class", "themeCommand");
            cellApply.ColSpan = 2;
            row2.Controls.Add(cellApply);

            cellApply.Controls.Add(_btnApply);
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.RegisterJQueryResource(Globals.ResourcePaths.JQueryLibraries.JQueryPlugins.JQueryCookie);
            base.RegisterJQueryResource("JQuery.WebKit.ThemeSwitcher.ThemeSwitcher.js");
            string script = string.Format("$('#{0}').button({{ label: 'Apply' }});", _btnApply.ClientID);
            this.Page.Resources.Add(new WebScriptResource(typeof(JPage), string.Empty, ResourceType.Javascript, script, LoadEvent.DocumentReady));

            base.Width = Unit.Percentage(100);

            _btnApply.Attributes.Add("onclick", string.Format("javascript:applyTheme($('#{0}').val());", _themeList.ClientID));
            _themeList.Attributes.Add("onclick", string.Format("javascript:showPreview($(this));", _themeList.ClientID));
            base.OnPreRender(e);
        }
    }
}
